// -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	SmartLib
//  File name:	ReadLock.cs
//  Developer:	Igor Solomatov
//  Created:	15.03.2013
// 
// -------------------- iSharePoint -------------------- //

using System;
using System.Threading;

namespace Smart
{
	/// <summary>
	/// Creates new read lock session
	/// </summary>
	public struct ReadLock : IDisposable
	{
		private ReaderWriterLockSlim _readerWriterLock;

		public ReadLock(ReaderWriterLockSlim readerWriterLock)
		{
			_readerWriterLock = readerWriterLock;

			readerWriterLock.EnterReadLock();
		}

		public void Dispose()
		{
			if (_readerWriterLock == null) throw new InvalidOperationException("Read lock is already freed!");

			_readerWriterLock.ExitReadLock();
			_readerWriterLock = null;
		}
	}
}